package com.nf.health.app.customview;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.nf.health.app.R;
import com.nf.health.app.utils.DensityUtil;

public class VerticalSlideRuler extends View {

	/**
	 * 分割块宽度(单位像素大小)
	 */
	private int unitWidth = 30;
	/**
	 * 总单位数
	 */
	private int totalCount = 250;
	private int textSize = 16;
	private int defaultValue = 0;
	private int startValue;
	private Paint paint;
	private Paint textPaint;
	private int viewHeight;//view的宽度
	private int smallDividerLineHeight;
	private int bigDividerLineHeight;
	private Rect rect;
	private float delay;
	public VerticalSlideRuler(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initAttrs(context, attrs);
		init(context);
	}

	public VerticalSlideRuler(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		
		initAttrs(context, attrs);
		init(context);
	}

	public VerticalSlideRuler(Context context,int defaultValue, int textSize, int totalCount, int unitWidth) {
		super(context);
		this.defaultValue = defaultValue;
		this.textSize = textSize;
		this.totalCount = totalCount;
		this.unitWidth = unitWidth;
		init(context);
	}
	
	private void initAttrs(Context context, AttributeSet attrs){
		TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.VerticalSlideRuler);
		defaultValue = typeArray.getInt(R.styleable.VerticalSlideRuler_default_value_v, defaultValue);
		textSize = typeArray.getDimensionPixelOffset(R.styleable.VerticalSlideRuler_text_size_v, textSize);
		totalCount = typeArray.getInt(R.styleable.VerticalSlideRuler_total_count_v, totalCount);
		unitWidth = typeArray.getInt(R.styleable.VerticalSlideRuler_unit_size_v, unitWidth);
		typeArray.recycle();
	}
	
	/**
	 * 初始化需指定此View的高度和宽度，否则onMeasure测量的值为0
	 */
	private void init(Context context) {
		textSize = DensityUtil.sp2px(context, textSize);
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);
		
		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(Color.BLACK);
		textPaint.setTextSize(textSize);
		
		
		smallDividerLineHeight = DensityUtil.dip2px(getContext(), 10);
		bigDividerLineHeight = DensityUtil.dip2px(getContext(), 20);
		
		rect = new Rect();
		
//		int screenHeight = ScreenUtils.getScreenHeight(getContext());
		int screenHeight = DensityUtil.dip2px(getContext(), 280);
		float halfwidth = screenHeight/2;
		int halfCount = (int) (halfwidth/unitWidth);
		delay = halfwidth%unitWidth;//计算偏离的像素值
		startValue = halfCount;
		
		viewHeight= unitWidth*totalCount+screenHeight;
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		
		setMeasuredDimension(DensityUtil.dip2px(getContext(), 70), viewHeight);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		
		for(int i=startValue+totalCount,j=0;i>=0;i--){
			if(i>totalCount){
				//小于0的刻度可以不用绘制
//				float startX = delay+unitWidth*(Math.abs(startValue)+i);
//				if(i==-10){
//					canvas.drawLine(startX, 0, startX, bigDividerLineHeight, paint);
//				}else{
//					canvas.drawLine(startX, 0, startX, smallDividerLineHeight, paint);
//				}
			}else{
				int count = j+Math.abs(startValue);
				float startY = delay+unitWidth*count;
				if(i%10==0){
					canvas.drawLine(0, startY, bigDividerLineHeight, startY, paint);
					
					String text = String.valueOf(i)+"cm";
					
					textPaint.getTextBounds(text, 0, text.length(), rect);
					
					canvas.drawText(String.valueOf(i)+"cm", bigDividerLineHeight, startY-rect.top/2, textPaint);
				}else{
					canvas.drawLine(0, startY, smallDividerLineHeight, startY, paint);
				}
				j++;
			}
		}
	}
	/**
	 * 
	 */
	public float getDelay(){
		return delay;
	}
	/**
	 * 获取单位刻度的宽度（像素值）
	 * @return
	 */
	public int getUnitWidth(){
		return unitWidth;
	}
	/**
	 * 获取总刻度数
	 * @return
	 */
	public int getTotalCount(){
		return totalCount;
	}
	/**
	 * 获取起始刻度值
	 */
	public int getStartValue(){
		return startValue;
	}
}
